package com.ghck.server.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ghck.server.dao.AppointmentMapper;
import com.ghck.server.model.Appointment;
import com.ghck.server.model.query.AppointmentQuery;
import com.ghck.server.model.response.DoctorAppointmentRes;
import com.ghck.server.model.response.DoctorRes;
import com.ghck.server.service.AppointmentService;
import com.ghck.server.service.DoctorService;

/**
 * 医生预约表  <br>
 * t_appointment <br>
 * v1.0 2016-05-01
 */
 @Service
 @Transactional
 public class AppointmentServiceImpl implements AppointmentService {
	
	private static final Logger logger = LoggerFactory.getLogger(AppointmentServiceImpl.class);
	
	@Autowired
	private AppointmentMapper appointmentMapper;
	
	@Autowired
	DoctorService doctorService;
	
	/**
 	*根据ID删除 医生预约表
 	*/
 	@Override
 	public Integer deleteAppointmentById(Integer id){
 		return appointmentMapper.deleteById(id);
 	}
 	
 	/**
 	*新增 医生预约表
 	*/
 	@Override
 	@Transactional
 	public Integer batchInsertAppointment(List<? extends Appointment> appointments){
		Integer count = appointmentMapper.batchInsert(appointments);
		return count;
 	}
 	/**
 	*新增 医生预约表
 	*/
 	@Override
 	@Transactional
 	public Integer insertAppointment(Appointment domain){
		Integer id = appointmentMapper.insert(domain);
		return id;
 	}

 	/**
 	*更新 医生预约表
 	*/
 	@Override
 	@Transactional
 	public Integer updateAppointment(Appointment domain){
		Integer count = appointmentMapper.update(domain);
		return count;
 	}
 	
 	/**
 	*根据ID查询 医生预约表
 	*/
 	public Appointment findAppointmentById(Integer id){
 		Appointment result = appointmentMapper.findById(id);
 		if(result!=null){
 			result.dealWith();
 		}
 		return result;
 	}

	/**
 	*通过query 查询 医生预约表
 	*/
	public List<DoctorAppointmentRes> findAppointmentByQuery(AppointmentQuery query){
		query.dealWith();
		List<Appointment> result = appointmentMapper.findByQuery(query);
		java.util.Collections.sort(result);
		List<DoctorAppointmentRes> resultRes = new ArrayList<>();
		Set<Integer> doctorIds = new HashSet<Integer>();
		if(CollectionUtils.isNotEmpty(result)) {
			for (Appointment appointment : result) {
				doctorIds.add(appointment.getDoctorId());
				appointment.dealWith();
			}
			List<DoctorRes> res = doctorService.findDoctorByUserIds(new ArrayList<>(doctorIds));
			Map<Integer, DoctorRes> map = new HashMap<Integer, DoctorRes>();	
			if (CollectionUtils.isNotEmpty(res)) {
				for (DoctorRes doctorRes : res) {
					map.put(doctorRes.getId(), doctorRes);
				}
			}
			
			for (Appointment appointment : result) {
				DoctorAppointmentRes res2 = new DoctorAppointmentRes();
				res2.setId(appointment.getId());
				res2.setDoctorId(appointment.getDoctorId());
				res2.setUserId(appointment.getUserId());
				res2.setAppointmentTime(appointment.getAppointmentTime());
				DoctorRes doctorRes = map.get(appointment.getDoctorId());
				res2.setAvatar(doctorRes.getAvatar());
				res2.setDepartmentName(doctorRes.getDepartmentName());
				res2.setHospitalName(doctorRes.getHospitalName());
				res2.setName(doctorRes.getName());
				res2.setTitleName(doctorRes.getTitleName());
				resultRes.add(res2);
			}
		}
		return resultRes;
	}	
	
	/**
 	*通过query count总条数
 	*/
 	public Integer countAppointmentByQuery(AppointmentQuery query){
 		query.dealWith();
 		Integer result = appointmentMapper.countByQuery(query);
 		return result;
 	}
 	
 	/**
 	* 通过ids查询
 	*/
 	public List<Appointment> findAppointmentByIds(List<Integer> ids){
 		List<Appointment> result = appointmentMapper.findByIds(ids);
		if(CollectionUtils.isNotEmpty(result)) {
			for (Appointment appointment : result) {
				appointment.dealWith();
			}
		}
		return result;
 	}
 	
}	